package com.banmao.CarlCamp.Day04;

import java.util.HashSet;

/**
 * https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/
 */
public class LC07 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        // Set集合？
        HashSet<ListNode> set = new HashSet<>();
        ListNode cur = headA;
        while (cur != null) {
            set.add(cur);
            cur = cur.next;
        }
        cur = headB;
        while (cur != null) {
            if (set.contains(cur)) {
                return cur;
            }
            set.add(cur);
            cur = cur.next;
        }
        return null;
    }

    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }

}
